package t2h.gui;


import java.io.IOException;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStoreException;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import t2h.activities.ListSongRecord;
import t2h.activities.ReadXmlFile;
import t2h.activities.ReadXmlFileWithoutLyric;
import t2h.activities.SearchSong;
import t2h.listsong.Compatibility;
import t2h.listsong.TwoRowListView;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListModel;
import com.sun.lwuit.plaf.UIManager;

public class SearchScreen extends Screen{
	private TextField txtSearch;
	private Button btn;
	private TwoRowListView listSong;
	Form f1 = new Form("Search Screen");
    //private Container demoPanel;
    //private SearchScreen searchscreen;
	//final ListModel model = new DefaultListModel();
	ListModel temp = new DefaultListModel();
	private static final int EXIT_COMMAND = 1;
	private static final Command exitCommand = new Command("Exit", EXIT_COMMAND);
	
	public SearchScreen(){
        //Command exitCommand = new Command("About",4);
        //f1.addCommand(exitCommand);
        //f1.addCommandListener(this);
	}
	
	/*private String[] CONTACTS_INFO = {
			"Nir.Vazana@Sun.COM",
		    "Tidhar.Gilor@Sun.COM",
		    "Iddo A.","Iddo.Arie@Sun.COM",
		    "Ari S.","Ari.Shapiro@Sun.COM",
		    "Chen F.","Chen.Fishbein@Sun.COM",
		    "Yoav B.","Yoav.Barel@Sun.COM",
		    "Moshe S.","Moshe.Sambol@Sun.COM",
		    "Keren S.","Keren.Strul@Sun.COM",
		    "Amit H.","Amit.Harel@Sun.COM",
		    "Arkady N.","Arcadi.Novosiolok@Sun.COM",
		    "Shai A.","Shai.Almog@Sun.COM"
		    };*/
    //private static final int ABOUT_COMMAND = 4;
	//private static final Command aboutCommand = new Command("About", ABOUT_COMMAND);	
	protected Container createPair(TextField textField, Button button, int minWidth, int minHeight) {
        Container pair;
        Dimension d = textField.getPreferredSize();
        d.setWidth(Math.max(d.getWidth(), minWidth));
        d.setHeight(Math.min(d.getHeight(), minHeight));
        textField.setPreferredSize(d);
        pair = new Container(new BorderLayout());
        pair.addComponent(BorderLayout.CENTER,textField);
        pair.addComponent(BorderLayout.EAST, button);
        return pair;
    }
	
	public Form startSearchScreen(){
		
		txtSearch = new TextField(10);
        
        
        Image img= null;
		try {
			img = Image.createImage("/source/Search.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		btn = new Button(img);
				        
        f1.addComponent(createPair(txtSearch,btn,20,10));
               
        //ReadXmlFile testre = new ReadXmlFile();
        ReadXmlFileWithoutLyric testre = new ReadXmlFileWithoutLyric();
        //SearchSong searchsongtest = new SearchSong();
        
        
        	ListModel testSearch = new DefaultListModel();
			try {
				testSearch = testre.getListKara();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
			//ListModel listsearch = new SearchSong().SearchSongByInput(txt.getText(), testSearch);
        	
			listSong = new TwoRowListView(testSearch);
	        f1.addComponent(listSong);
        
        f1.setScrollable(false);
		
		ActionListener listener = new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				//ReadXmlFile testre = new ReadXmlFile();
				ReadXmlFileWithoutLyric testre = new ReadXmlFileWithoutLyric();
		        //SearchSong searchsongtest = new SearchSong();
		        
		        try {
		        	ListModel testSearch = testre.getListKara();
		        	
					ListModel listsearch = new SearchSong().SearchSongByInput(txtSearch.getText(), testSearch);
		        	
					listSong = new TwoRowListView(listsearch);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

		        f1.addComponent(listSong);
			}
		};
		

		f1.addCommand(new Command("Exit") {
            public void actionPerformed(ActionEvent evt) {
            	Display.getInstance().exitApplication();
                }
            });
		btn.addActionListener(listener);
        return f1;
	}

	
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String text;
        text = "This is soft T2H Karaoke";

        Dialog.show(Compatibility.toLowerCaseIfFT("About"),
                text,
                new Command(Compatibility.toUpperCaseIfFT("Continue")),
                null,
                Dialog.TYPE_INFO,
                null,
                5000L);
        T2HKaraoke.getMainForm().show();
	}

	public String getName() {
		// TODO Auto-generated method stub
		return "Search Screen";
	}
	
	public void executeDemo(Form f){
		txtSearch = new TextField(10);
        
        
        Image img= null;
		try {
			img = Image.createImage("/source/Search.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		btn = new Button(img);
				        
        f.addComponent(createPair(txtSearch,btn,20,10));
               
        //f1.addComponent(listSong);
        
        f.setScrollable(false);
        ReadXmlFile testre = new ReadXmlFile();
        //SearchSong searchsongtest = new SearchSong();
        
        ListModel testSearch = new DefaultListModel();
		try {
			testSearch = testre.getListKara();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		listSong = new TwoRowListView(testSearch); 
        f.addComponent(listSong);        	
		
		ActionListener listener = new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				ReadXmlFile testre = new ReadXmlFile();
		        
		        try {
		        	ListModel testSearch = testre.getListKara();		        	
					ListModel listsearch = new SearchSong().SearchSongByInput(txtSearch.getText(), testSearch);
					listSong.setNewModel(listsearch);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		

		f.addCommand(new Command("Exit") {
            public void actionPerformed(ActionEvent evt) {
            	Display.getInstance().exitApplication();
                }
            });
		
		btn.addActionListener(listener);        
	}
	
	
}
